<?php
/**
 * class provides operations with xml files
 */
    class classImageViewerXMLService{
    /**
     * function gets into IO variables $sItemName, $sItemDescription values from 
     * XML file by name <i>info.xml</i> which is saved in $dirname directory
     */ private $xmlDoc;
        
        function __construct(){
            $this->xmlDoc = new DOMDocument();
	}
        
        public function getXMLDataDOM($dir,$item,&$sItemName,&$sItemDescription){
            $result = false;
            $this->xmlDoc = new DOMDocument();
            $filename = $dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_;
            if(!file_exists($filename))
                return $result;
            $this->xmlDoc->load($filename);
            $items = $this->xmlDoc->getElementsByTagName("item");
            
            foreach($items as $my_item)
            {
                if((string)$my_item->getAttribute("name")==$item){
                    $caption = $my_item->getElementsByTagName("caption");
                    $description = $my_item->getElementsByTagName("description");

                    $sItemName = $caption->item(0)->nodeValue;
                    $sItemDescription = $description->item(0)->nodeValue;//."<br>";

                    $result = true;
                }
            }
            if(!$result)
            {
                $sItemName = " ";
                $sItemDescription = " ";
            }
            return $result;
        }
    
        public function UpdateXMLDataDOM($dir,$item,$newitem,$sItemName,$sItemDescription){
            $this->xmlDoc = new DOMDocument();
            $this->xmlDoc->load($dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_);

            $items = $this->xmlDoc->getElementsByTagName("item");
        
            $result = false;
            foreach($items as $my_item)
            {
                if((string)$my_item->getAttribute("name")==$item){
                    $caption = $my_item->getElementsByTagName("caption");
                    $description = $my_item->getElementsByTagName("description");
                    
                    $my_item->setAttribute ("name",$newitem );
                    $caption->item(0)->nodeValue = $sItemName;
                    $description->item(0)->nodeValue = $sItemDescription;

                    $result = true;
                    break;
                }
            }
        
            $this->xmlDoc->save($dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_);
            return $result;
        }
        
        public function DeleteXMLDataDOM($dir,$item){
            $this->xmlDoc = new DOMDocument();
            $this->xmlDoc->load($dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_);

            $root = $this->xmlDoc->documentElement;
            $items = $this->xmlDoc->getElementsByTagName("item");
        
            $result = false;
            foreach($items as $my_item)
            {
                if((string)$my_item->getAttribute("name")==$item){
                    $root->removeChild($my_item);
                    $result = true;
                    break;
                }
            }
        
            $this->xmlDoc->save($dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_);
            return $result;
        }
        
        public function InsertXMLDataDOM($dir,$item,$sItemName,$sItemDescription){
            $this->xmlDoc = new DOMDocument();
            $filename = $dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_;
            if(!file_exists($filename))
                $this->CreateXMLDataDOM($dir);
                
            $this->xmlDoc->load($filename);
            $items = $this->xmlDoc->getElementsByTagName("item");
            
            if($items->length == 0)
                $this->CreateItemXMLDataDOM($dir,$item,$sItemName,$sItemDescription);
            else
            {
                $root_child = $this->CreateItem($item,$sItemName,$sItemDescription);

                foreach($items as $my_item)
                {
                    $my_item->parentNode->insertBefore($root_child, $my_item);
                    break;    
                }
            }
            
            $this->xmlDoc->save($dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_);
        }
        
        public function CreateXMLDataDOM($dir){
            $this->xmlDoc = new DOMDocument('1.0', 'UTF-8');
            
            $root = $this->xmlDoc->createElement("root");
            $this->xmlDoc->appendChild($root);
                    
            $this->xmlDoc->save($dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_);
        }
        
        private function CreateItemXMLDataDOM($dir,$item,$sItemName,$sItemDescription){
            $this->xmlDoc = new DOMDocument('1.0', 'UTF-8');
            
            $root = $this->xmlDoc->createElement("root");
            $this->xmlDoc->appendChild($root);
            
            $root_child = $this->CreateItem($item,$sItemName,$sItemDescription);
            $root->appendChild($root_child);
                    
            //$xmlDoc->save($dir.'/'._IMAGEVIEWER_INFO_XML_FILENAME_);
        }
        
        private function CreateItem($item,$sItemName,$sItemDescription){
            $root_child = $this->xmlDoc->createElement('item');
            
            $root_attr1 = $this->xmlDoc->createAttribute('name'); 
            $root_child->appendChild($root_attr1); 
    
            $root_text = $this->xmlDoc->createTextNode($item); 
            $root_attr1->appendChild($root_text);
            
            $root_child1 = $this->xmlDoc->createElement('caption',$sItemName);
            $root_child->appendChild($root_child1);
            
            $root_child2 = $this->xmlDoc->createElement('description',$sItemDescription);
            $root_child->appendChild($root_child2);
            
            return $root_child;
        }
    }
?>
